﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace WpfWordsLearner.Views
{
	/// <summary>
	/// Interaction logic for ImportView.xaml
	/// </summary>
	public partial class ImportView : Window
	{
		public bool IsImported { get; private set; }
		public ImportView()
		{
			InitializeComponent();
			IsImported = false;
			txtNewDict.Text = "Dictionary2.xml";
		}

		private void btnFile_Click(object sender, RoutedEventArgs e)
		{
			Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
			ofd.CheckFileExists = true;
			if (cmbFileType.Text.Contains("Xml"))
				ofd.Filter = "Xml file(*.xml)|*.xml";
			else
				ofd.Filter = "Text document(*.txt)|*.txt";
			ofd.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
			if (ofd.ShowDialog() == true)
			{
				txtPath.Text = ofd.FileName;
			}
		}

		private void btnImport_Click(object sender, RoutedEventArgs e)
		{
			string selctedVal = cmbFileType.Text;
			if (chkImp.IsChecked == false)
			{
				if (string.IsNullOrEmpty(txtPath.Text))
					return;

				ImportManager import = new ImportManager(txtPath.Text, selctedVal.Contains("Xml") ? ImportSource.XML : ImportSource.File);
				import.Completed += new EventHandler(import_Completed);
				import.Run();
			}
			else if (chkImp.IsChecked == true)
			{
				ImportManager import = new ImportManager(txtPath.Text, selctedVal.Contains("Xml") ? ImportSource.XML : ImportSource.File);
				import.Completed += new EventHandler(import_Completed);
				string fpath = System.IO.Path.Combine(App.pathToContent, txtNewDict.Text);
				import.Run(fpath);
			}
		}

		void import_Completed(object sender, EventArgs e)
		{
			Dispatcher.BeginInvoke(new Action(() =>
				{
					IsImported = true;
					this.Close();
					MessageBox.Show(this, "Import successfully done");
				}), System.Windows.Threading.DispatcherPriority.Normal);
		}

		private void btnCancel_Click(object sender, RoutedEventArgs e)
		{
			this.Close();
		}
	}
}
